package com.gen.info.generate_cluster_information.util;

import com.gen.info.generate_cluster_information.mode.ConnectionInfo;
import lombok.extern.slf4j.Slf4j;

import java.sql.*;

@Slf4j
public class DBUtil2 {
private static Connection connection = null;
private static PreparedStatement preparedStatement = null;
private static   Statement stmt = null;
public  static Connection getConnetcion(ConnectionInfo connectionInfo){
    try {
        Class.forName(connectionInfo.getJdbc_drive());
        String username = connectionInfo.getUsername()+"@"+connectionInfo.getTenant_name()+
                "#"+connectionInfo.getCluster_name();
        //获取数据库连接
        connection= DriverManager.getConnection(connectionInfo.getUrl(),username,connectionInfo.getPassword()==null?"":connectionInfo.getPassword());
    } catch (ClassNotFoundException | SQLException e) {
        e.printStackTrace();
    }
    if (null!=connection) log.info("租户"+connectionInfo.getTenant_name()+
            "的"+connectionInfo.getUsername()+"用户>>连接数据库成功!!!!!!!!");
    else log.info("连接数据库失败!!!!!!!!");;
   return connection;
}
    public static  Statement getCreateStatement(Connection connection) throws SQLException {
        stmt = connection.createStatement();
        return  stmt;
    }
    public static  PreparedStatement getPreparedStatement(Connection connection,String sql) throws SQLException {
        final PreparedStatement preparedStatement = connection.prepareStatement(sql);
        return  preparedStatement;
    }
public static  void closeConnection(){
    try {
        if (connection!=null){
            connection.close();
        }
        if (preparedStatement!=null){
            preparedStatement.close();
        }
       if (stmt!=null){
           stmt.close();
       }
    } catch (SQLException throwables) {
        throwables.printStackTrace();
    }

}
}
